package com.bfb.adapters;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bfb.R;

/**
 * The custom adapter for the dish types grid view. Extends {@link BaseAdapter}.
 * 
 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
 * 
 */
public class DishTypeGridAdapter extends BaseAdapter {

	/** The dish type names */
	String[] dishTypeNames;

	/** The dish type drawables */
	TypedArray dishTypeDrawables;

	Context context;

	/**
	 * Constructor takes as input the lists of names and drawables and the
	 * context.
	 * 
	 * @param dishTypeNames
	 * @param dishTypeDrawables
	 * @param context
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	public DishTypeGridAdapter(String[] dishTypeNames,
			TypedArray dishTypeDrawables, Context context) {
		this.dishTypeNames = dishTypeNames;
		this.dishTypeDrawables = dishTypeDrawables;
		this.context = context;
	}

	@Override
	public int getCount() {
		return dishTypeNames.length;
	}

	@Override
	public Object getItem(int position) {
		return dishTypeNames[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Overridden getView method of {@link BaseAdapter}. Inflates the view of
	 * the grid item. It is composed of a text view compound drawable with the
	 * name and image of the dish type.
	 * 
	 * @param position
	 * @param convertView
	 * @param parent
	 * 
	 * @return {@link View}
	 * 
	 * @author Rawan Khalil (rawan.tkhalil@gmail.com)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View myView = convertView;

		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			myView = inflater.inflate(R.layout.dish_types_griditem, null);

			TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
			tv.setText(dishTypeNames[position]);
			Drawable d = dishTypeDrawables.getDrawable(position);
			d.setBounds(0, 0, 80, 80);
			tv.setCompoundDrawables(null, d, null, null);
		}

		return myView;
	}
}
